package com.kele.controller;

import com.kele.domain.Blog;
import com.kele.service.BlogService;
import com.kele.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/blog")
@Api(value = "博客模块",tags = {"博客类管理接口"})
public class BlogController {


    @Resource
   private BlogService blogService;

    /**
     * 发布博客
     * @param blog
     * @return
     */
    @RequestMapping(value = "/releaseBlog",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "发布博客",notes = "发布博客类基本属性")
    @ApiImplicitParam(name = "blog",value = "博客类")
    public ResponseData releaseBlog(Blog blog){
        if(blogService.addBlog(blog)>0){
            return ResponseData.success("发布成功",blog);
        }else {
            return ResponseData.fail("发布失败");
        }
    }


    /**
     * 展示博客到主页面上
     * @param model 转发list集合
     * @return 返回一个集合 包含着要展示的数据
     */
    @RequestMapping(value = "/showBlogs",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "将博客显示到页面上",notes = "随机选取六篇博客显示")
    public ResponseData showBlogs(Model model){
        List<Blog> blogs = blogService.showBlogs();

        //新的list集合储存取出来的数据
        List<Blog> list=new ArrayList<Blog>();
        if (blogs.size()>=6){
        Random random=new Random();
        for (int i = 0; i < 6; i++) {
            int intRandom=random.nextInt(blogs.size()-1);
            list.add(blogs.get(intRandom));
            blogs.remove(blogs.get(intRandom));
        }

        model.addAttribute("blogs",list);
        return ResponseData.success("查询成功",list);
        }
        model.addAttribute("blogs",list);
        return ResponseData.success("查询成功??",list);
    }

        @RequestMapping(value = "/findById",method = RequestMethod.GET)
        @ResponseBody
        @ApiOperation(value = "查询一篇博客",notes = "根据id查询博客所有属性")
        @ApiImplicitParam(name = "id",value = "博客id",required = true)
        public ResponseData findBlogs( int  id){
            Blog blog = blogService.findBlogById(id);
            return ResponseData.success("查询成功",blog);
        }
//
//    //查询所有博客信息
//    @RequestMapping(value = "/findBlogs",method = RequestMethod.GET)
//    @ResponseBody
//    @ApiOperation(value = "查询所有博客类信息",notes = "查询所有博客类信息")
//    public List<Blog> findBlogs( Model model){
//        List<Blog> blogs = blogService.findBlogs();
//        System.out.println(blogs);
//        model.addAttribute("blog",blogs);
//        return blogs;
//    }
//
//    //根据博客属性查询博客信息
//    @RequestMapping(value = "/findByTitle",method = RequestMethod.GET)
//    @ApiOperation(value = "查询博客类",notes = "根据博客属性查询博客类")
//    @ResponseBody
//    @ApiImplicitParam(name = "field",value = "博客属性",required = true,paramType = "query")
//    public ResponseData<Blog> findByTitle(String field){
//        List<Blog> blogs=blogService.findByTitle(field);
//        if (blogs!=null){
//            return ResponseData.success("查询成功",blogs);
//        }else{
//            return ResponseData.fail("该类博客不存在或已被删除");
//        }
//    }
//
//
//    //根据id删除博客文章
//    @RequestMapping(value = "/deleteBlog",method = RequestMethod.DELETE)
//    @ApiOperation(value = "删除博客类",notes = "根据用户id删除博客类")
//    @ResponseBody
//    @ApiImplicitParam(name = "id",value = "博客id",required = true,dataType = "int")
//    public ResponseData deleteBlog(int id){
//        if (blogService.deleteBlog(id)>0){
//            return ResponseData.success("删除成功");
//        }else {
//            return ResponseData.fail("删除失败或id不存在");
//        }
//    }
//
//    //点赞（+1）
//    @RequestMapping(value = "/adddianzan",method = RequestMethod.GET)
//    @ApiOperation(value = "点赞数+1",notes = "根据id对博客进行点赞")
//    @ResponseBody
//    @ApiImplicitParam(name = "id",value = "博客类id",required = true,dataType = "int")
//    public ResponseData adddianan(int id){
//        if (blogService.adddianzan(id)>0){
//            return ResponseData.success("点赞成功，点赞数+1");
//        }else {
//            return ResponseData.fail("点赞失败");
//        }
//
//    }
//
//    //点赞（-1）
//    @RequestMapping(value = "/subdianzan",method = RequestMethod.GET)
//    @ApiOperation(value = "点赞数-1",notes = "根据id对博客进行取消点赞")
//    @ResponseBody
//    @ApiImplicitParam(name = "id",value = "博客类型id",required = true,dataType = "int")
//    public ResponseData subdianzan(int id) {
//        if (blogService.subdianzan(id) > 0) {
//            return ResponseData.success("取消点赞成功,点赞数-1");
//        } else {
//            return ResponseData.fail("点赞失败");
//        }
//    }
//
//
//
//        //点赞
//    @RequestMapping(value = "/dianzan",method = RequestMethod.GET)
//    @ApiOperation(value = "点赞功能",notes = "根据博客id对博客进行点赞")
//    @ResponseBody
//    @ApiImplicitParam(name = "id",value = "博客id",required = true,dataType = "int")
//    public ResponseData dianzan(int id) {
//        if (id == id) {
//            if (blogService.adddianzan(id) > 0) {
//                return ResponseData.success("点赞成功，点赞数+1");
//            } else {
//                return ResponseData.fail("点赞失败");
//            }
//        } else {
//            if (blogService.subdianzan(id) > 0) {
//                return ResponseData.success("取消点赞成功，点赞数-1");
//            } else {
//                return ResponseData.fail("取消点赞失败");
//            }
//        }
//    }
}
